﻿<?php require_once(dirname(__FILE__)."/../config/config.php"); ?>
<?php
/**
 * 分页方法.传入参数后可以直接生成分页.
*@param $p		当前页码
*@param $where	where查询语句,以and 开始.当$where不以and开始时,默认是传来可执行的SQL语句.
*@param $table	表名
*@param $pk		主键列名
*@param $pageSize	每页显示数量
*/
function page($p, $where, $table, $pk, $pageSize) {
	$Page['totalResult'] = 0;
	$Page['totalPages'] = 0;
	$Page['currentPage'] = $p;
	$Page['pageSize'] = $pageSize;
	$Page['pageStep'] = 10;
	if (substr($where, 0, 3) != 'and') {
		$sql = $where;
	} else {
		$sql = "select count(message_id) num from zy_message where 1=1 and " . $where;
	}
	$rs=mysql_query($sql);
	if($array=mysql_fetch_array($rs)){
	$Page['totalResult'] = $array[num];
	$Page['totalPages'] = ceil($Page['totalResult'] / $Page['pageSize']);
	if ($Page['currentPage'] - 5 <= 1) {
		$Page['begin'] = 1;
		if ($Page['totalPages'] > 10) {
			$Page['end'] = 10;
		} else {
			$Page['end'] = $Page['totalPages'];
		}

	} else
		if ($Page['currentPage'] + 4 >= $Page['totalPages']) {
			$Page['end'] = $Page['totalPages'];
			if ($Page['totalPages'] > 10) {
				$Page['begin'] = $Page['totalPages'] - $Page['pageStep'] + 1;
			} else {
				$Page['begin'] = 1;
			}
		} else {
			$Page['begin'] = $Page['currentPage'] - 5;
			$Page['end'] = $Page['currentPage'] + 4;
		}
	return $Page;
	}
}
?>
